package com.silence.whereeat.register;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.silence.whereeat.R;
import com.silence.whereeat.main.MainActivity;
import com.silence.whereeat.model.UserInfo;
import com.silence.whereeat.utils.CommonUrl;
import com.silence.whereeat.utils.Content;
import com.silence.whereeat.utils.HttpClientPost;

/**
 * 注册界面
 * Created by KIM on 2015/7/25.
 */
public class RegisterActivity extends Activity {

    private static final String TAG = "RegisterActivity";

    private EditText nameEt;
    private EditText passwordEt;
    private EditText repasswordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbgcolor));
        }

        nameEt = (EditText) findViewById(R.id.register_name);
        passwordEt = (EditText) findViewById(R.id.register_password);
        repasswordEt = (EditText) findViewById(R.id.register_repassword);
        Button btnRegister = (Button) findViewById(R.id.register_btn);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String n = nameEt.getText().toString().trim();
                String p = passwordEt.getText().toString().trim();
                String rep = repasswordEt.getText().toString().trim();
                if (n.isEmpty()) {
                    nameEt.setError("此项不能为空！");
                } else if (p.isEmpty()) {
                    passwordEt.setError("此项不能为空！");
                } else if (rep.isEmpty()) {
                    repasswordEt.setError("此项不能为空！");
                } else if (!p.equals(rep)) {
                    repasswordEt.setError("两次输入密码不同！");
                } else {
                    UserInfo userInfo = new UserInfo(n, p);
                    loadToMainActivity(userInfo);
                }
            }
        });
    }

    public void loadToMainActivity(UserInfo userInfo) {
        new AsyncTask<UserInfo, Void, String>() {
            @Override
            protected String doInBackground(UserInfo... params) {
                HttpClientPost httpClientPost = new HttpClientPost(CommonUrl.URL_REGISTER);
                String result = httpClientPost.getLoginResult(params[0]);
                Log.i(TAG, "run -----" + result);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (s.equals(Content.STR_SUCCESS)) {
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    passwordEt.setText("");
                    repasswordEt.setText("");
                    nameEt.setFocusable(true);
                    nameEt.setFocusableInTouchMode(true);
                    nameEt.requestFocus();
                    nameEt.setError("注册失败,用户名已存在!");
//                Toast.makeText(RegisterActivity.this, "注册失败!", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute(userInfo);
    }
}
